package com.fengshang.wedding.domain.model.internal;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.fengshang.wedding.domain.Constants;
import com.fengshang.wedding.domain.model.api.IMessage;
import com.fengshang.wedding.domain.model.api.Message;

/**
 * 
 *@author gembin@gmail.com
 * 
 */
@Entity(name = Constants.MESSAGE_ENTITY_NAME)
@NamedQuery(name = Constants.MESSAGE_FIND_ALL, query = "SELECT m FROM Message m ORDER BY m.postDate DESC")

public class MessageEntity implements IMessage, Serializable {
	private static final long serialVersionUID = -2007010328918025518L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@Column(name="title",nullable=false)
	private String title;

	@Temporal(value = TemporalType.TIMESTAMP)
	private Date postDate;

	@Column(name="content",nullable=false)
	private String content;
	
	@ManyToOne
	@JoinColumn(name = "TOPIC_ID", nullable = false)
	@OrderBy("postDate DESC")
	private TopicEntity topic;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getPostDate() {
		return postDate;
	}

	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public TopicEntity getTopic() {
		return topic;
	}

	public void setTopic(TopicEntity topic) {
		this.topic = topic;
	}

	@Transient
	public IMessage importData(IMessage message){
		if (message != null){
			this.title = message.getTitle()==null?title:message.getTitle();
			this.postDate = message.getPostDate()==null?postDate:message.getPostDate();
			this.content = message.getContent()==null?content:message.getContent();
		}
		return this;
	}
	
	@Transient
	public IMessage getValueObject(){
		  return new Message(this); 
	}

}
